package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsModel;
import net.realsee.domain.wcs.data.bo.WcsModelBo;
import net.realsee.domain.wcs.data.vo.WcsModelVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsModelMapper;
import net.realsee.service.wcs.data.IWcsModelService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 机种维护系统内需要达的机种信息Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsModelServiceImpl extends ServiceImpl<WcsModelMapper, WcsModel> implements IWcsModelService {

    private final WcsModelMapper baseMapper;

    /**
     * 查询机种维护系统内需要达的机种信息
     */
    @Override
    public WcsModelVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询机种维护系统内需要达的机种信息列表
     */
    @Override
    public TableDataInfo<WcsModelVo> selectVoPage(WcsModelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsModel> lqw = buildVoQueryWrapper(bo);
        Page<WcsModelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询机种维护系统内需要达的机种信息列表
     */
    @Override
    public List<WcsModelVo> selectVoList(WcsModelBo bo) {
        LambdaQueryWrapper<WcsModel> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsModel> buildVoQueryWrapper(WcsModelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsModel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsModel::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getModelCode()), WcsModel::getModelCode, bo.getModelCode());
        lqw.like(StringUtils.isNotBlank(bo.getModelName()), WcsModel::getModelName, bo.getModelName());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsModel::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsModel::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsModel::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsModel::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsModel::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsModel::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsModel::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsModel::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsModel::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsModel::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsModel::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsModel::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增机种维护系统内需要达的机种信息
     */
    @Override
    public Boolean insertByBo(WcsModelBo bo) {
        WcsModel add = BeanUtil.toBean(bo, WcsModel.class);
        return this.save(add);
    }

    /**
     * 修改机种维护系统内需要达的机种信息
     */
    @Override
    public Boolean updateByBo(WcsModelBo bo) {
        WcsModel update = BeanUtil.toBean(bo, WcsModel.class);
        return this.updateById(update);
    }

    /**
     * 批量删除机种维护系统内需要达的机种信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
